/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.entry;

import com.genia.toolbox.web.gwt.basics.client.GwtHelper;
import com.genia.toolbox.web.gwt.basics.client.log.impl.PanelLogger;
import com.genia.toolbox.web.gwt.form.client.Constants;
import com.genia.toolbox.web.gwt.form.client.CssNames;
import com.genia.toolbox.web.gwt.form.client.manager.FormController;
import com.genia.toolbox.web.gwt.form.client.process.ProcessForm;
import com.genia.toolbox.web.gwt.form.client.value.DataIdentifier;
import com.genia.toolbox.web.gwt.form.client.value.impl.DataIdentifierImpl;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point for the Form API presentation.
 */
public class FormEntryPoint
    implements EntryPoint
{

  /**
   * start a form described in the given element.
   * 
   * @param element
   *          the {@link Element} that will host the form and that contains the
   *          form informations.
   */
  private void startForm(Element element)
  {
    String formIdentifier = GwtHelper.getParameter(Constants.FORM_IDENTIFIER_PARAMETER_NAME);
    String identifier = GwtHelper.getParameter(Constants.DATA_IDENTIFIER_PARAMETER_NAME);
    String backUrl = GwtHelper.getParameter(Constants.BACK_URL_PARAMETER_NAME);
    NodeList<Element> inputs = element.getElementsByTagName("input");
    for (int i = 0; i < inputs.getLength(); i++) {
      Element input = inputs.getItem(i);
      if ("formIdentifier".equals(input.getAttribute("name"))) {
        formIdentifier = input.getAttribute("value");
      }
      if ("dataIdentifier".equals(input.getAttribute("name"))) {
        identifier = input.getAttribute("value");
      }
      if (Constants.BACK_URL_PARAMETER_NAME.equals(input.getAttribute("name"))) {
        backUrl = input.getAttribute("value");
      }
    }
    final String finalFormIdentifier = formIdentifier;
    final DataIdentifier dataIdentifier = new DataIdentifierImpl(identifier);
    final String finalBackUrl = backUrl;
    
    String id = Document.get().createUniqueId();
    element.setId(id);

    final VerticalPanel mainPanel = new VerticalPanel();
    final HorizontalPanel buttonsPanel = new HorizontalPanel();
    mainPanel.addStyleName(CssNames.MAIN_PANEL_STYLE_NAME);
    buttonsPanel.addStyleName(CssNames.BUTTON_PANEL_STYLE_NAME);
    RootPanel rootPanel = RootPanel.get(id);
    rootPanel.add(mainPanel);
    rootPanel.add(buttonsPanel);

    ProcessForm.addWaitingMessage(mainPanel);
    DeferredCommand.addCommand(new Command()
    {
      public void execute()
      {
        new FormController(mainPanel, buttonsPanel, finalFormIdentifier, dataIdentifier, finalBackUrl).init();
      }
    });
  }



  /**
   * the method called to initialize the application.
   */
  public void onModuleLoad()
  {
    final PanelLogger logger = new PanelLogger();
    GwtHelper.logger = logger;
    RootPanel.get().add(logger.getErrorPanel());
    if (GWT.isScript()) {
      GwtHelper.addUncaughtExceptionHandler();
    }

    for (Element element : GwtHelper.getElementsByClassName("div", "gwtFormApi")) {
      startForm(element);
    }
  }
}
